﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblCategoriesDAO
    {
          private DBService _db = null;
          public TblCategoriesDAO(string ConnectionString)
         {
            _db = new DBService(ConnectionString);
         }
          public DataTable Insert(TblCategories cate, int Mode)
          {
              DataTable dt = null;
              try
              {
                 
                  dt = _db.ExeSP_DataTable("sp_TblCateIn",
                      new string[] { "@CateID", "@CateName", "@CateDescription", "@ImageURL", "@IDGroup","Mode"},
                      new object[] { cate.CateID, cate.CateName, cate.CateDescription, cate.ImageURL, cate.IDGroup, Mode },
                      CommandType.StoredProcedure); 

                 
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return dt;
          }

          public DataTable GetAll()
          {
              DataTable Cate = null;
              try
              {
                  Cate = _db.ExeSP_DataTable("select * from TblCategories order by CateID desc", false, CommandType.Text);

              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return Cate;
          }

          public DataTable GetGroup()
          {
              DataTable Group = null;
              try
              {
                  Group = _db.ExeSP_DataTable("select * from TblGroup", false, CommandType.Text);

              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return Group;
          }

          public TblCategories GetCateByID(int CateID)
          {
              TblCategories cate = new TblCategories();
              try
              {
                  DataTable dt = null;
                  dt = _db.ExeSP_DataTable("select * from TblCategories where CateID = @CateID",
                      new string[] { "@CateID" },
                      new object[] { CateID }
                      , CommandType.Text);
                  foreach (DataRow dr in dt.Rows)
                  {
                      cate.CateID = CateID;
                      cate.CateName = dr["CateName"].ToString();
                      cate.CateDescription = dr["CateDescription"].ToString();
                      cate.ImageURL = dr["ImageURL"].ToString();
                      cate.IDGroup = Convert.ToInt32(dr["IDGroup"]);
                     
                  }
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return cate;
          }
          public bool check_cate(string CateName)
          {
              bool flag = false;
              try
              {
                  DataTable dt = _db.ExeSP_DataTable("select * from TblCategories where CateName = @CateName",
                     new string[] { "@CateName" },
                     new object[] { CateName},
                     CommandType.Text);
                  if (dt.Rows.Count > 0)
                  {
                      flag = true;
                  }
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return flag;
          }

          public DataTable GetCateByGroupID(int IDGroup)
          {
              DataTable cate = null;
              try
              {
                  cate = _db.ExeSP_DataTable("select * from TblCategories where IDGroup = @IDGroup",
                      new string[] { "@IDGroup" },
                      new object[] { IDGroup }
                      , CommandType.Text);
                  //foreach (DataRow dr in dt.Rows)
                  //{
                  //    cate.CateID = Convert.ToInt32(dr["CateID"]);
                  //    cate.CateName = dr["CateName"].ToString();
                  //    cate.CateDescription = dr["CateDescription"].ToString();
                  //    cate.ImageURL = dr["ImageURL"].ToString();
                  //    cate.IDGroup = IDGroup;

                  //}
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return cate;
          }
    }
}
